<?php
// Compacts the weights in our menu after deleting items
function gcmsNav_compact($parent='IS NULL') {
	global $db;
	if ($parent != 'IS NULL')
		$parent = '= ' . $db->quote($parent);
	$menu = $db->fetchAll("SELECT * FROM menu WHERE parent $parent ORDER BY weight");
	$count = 0;
	foreach ($menu as $v) {
		if ($v['weight'] != $count)
			$db->update('menu', array('weight' => $count), "parent $parent AND id = " . $v['id']);
		$count++;
	}
}

function gcmsNav_onLoad($db) {
	if (!$GLOBALS['authNamespace']->admin)
		return;
	if (isset($_GET['action']) && $_GET['action'] == 'delete' && $_GET['item'] > 2) {
		$db->delete('menu', 'id = ' . $db->quote($_GET['item']));
		gcmsNav_compact();
	}
	elseif (isset($_POST['save']) && !empty($_POST['name'])) {
		if (!empty($_GET['id']))
			$db->update('menu', array('name' => $_POST['name'], 'link' => $_POST['link']), 'id = ' . $db->quote($_REQUEST['id']));
		else
			$db->insert('menu', array('name' => $_POST['name'], 'link' => $_POST['link'], 'parent' => empty($_POST['parent']) ? new Zend_Db_Expr('NULL') : $_POST['parent'], 'weight' => 127));
	}
}

function gcmsNav_head($db, &$headers, $translate) {
	if (isset($_GET['action']) && $GLOBALS['authNamespace']->admin)
		$headers->headScript()->appendFile('./modules/gcmsNav/nav.js');
}

// GCMS calls main when its rendering the page content
function gcmsNav_main($db, &$view, $translate) {
	global $config;
	if (!$GLOBALS['authNamespace']->admin)
		die("Session expired.  Please login.");
	$menu = $db->fetchAll("SELECT * FROM menu WHERE parent IS NULL ORDER BY weight,id");
	switch ($_GET['action']) {
		case 'edit':
			$db->fetchRow("SELECT * FROM menu WHERE id = ?", $_GET['id']);
		case 'add':
			$poptions = array("" => "[No Parent]");
			foreach ($menu as $v) {
				if ($v['id'] != $_GET['id'])
					$poptions[$v['id']] = $v['name'];
				if ($v['id'] == $_GET['id'])
					$view->assign('item', $v);
			}
			$view->assign('poptions', $poptions);
			$view->display('file:../../../modules/gcmsNav/edit.tpl');
			break;
		default:
			$cmenu = $db->fetchAll("SELECT * FROM menu WHERE parent = 2 ORDER BY weight,id");
			$view->assign('menu', $menu);
			$view->assign('cmenu', $cmenu);
			$view->display('file:../../../modules/gcmsNav/manage.tpl');
	}
}

// Add an admin menu item
// Returns an array w the first value as the title and menu options after that
function gcmsNav_adminMenu($translate) {
	$stack = array();
	$stack[] = $translate->_("Navigation Menu Config");
	$stack['action=manage'] = $translate->_("Edit Menu");
	return $stack;
}

/**
 * Generates a site menu.
 *
 * @param array $args Arguments to pass to the module in the form of an assoc array.
 */
function insert_gcmsNav($args) {
	global $db, $translate;
	$adata = $db->fetchAll("SELECT * FROM menu WHERE parent IS NULL ORDER BY weight,id");
	$first = true;
	echo "<ul id=\"nav\">\n";
	foreach ($adata as $k => $v) {
		if (empty($v['link'])) {
			$first = true; // Reset our first flag for our submenus
			echo "<ul class=\"submenu\">\n<li class=\"subheader\"><b>$v[name]</b></li>\n";
			$rs = $db->query("SELECT name,link FROM menu WHERE parent = '$v[id]' ORDER BY weight,id DESC");
			while ($item = $rs->fetch()) {
				echo "<li"; if ($first == true) { echo ' class="first"'; $first = false; } echo "><a href=\"" . htmlspecialchars($item['link']) ."\">" . htmlspecialchars($item['name']) . "</a></li>\n";
			}
			echo "</ul>\n";
		} else {
			echo "<li"; if ($first == true) { echo ' class="first"'; $first = false; } echo "><a href=\"" . htmlspecialchars($v['link']) . "\""; if ($_GET['mod'] == $v['link']) { echo ' id="active_menu"'; } echo ">" . htmlspecialchars($v['name']) . "</a></li>\n";
		}
	}
	echo "</ul>\n";
	// Temp hack till AJAX integration
	if ($GLOBALS['authNamespace']->admin)
		echo "<script type=\"text/javascript\">$('nav').grab(new Element('li', {html:'<a href=\"index.php?mod=admin\">" . $translate->_("Admin") . "</a>'}));</script>";
}
?>